package com.demo.controller;

import com.demo.dto.DepartmentPageQueryDTO;
import com.demo.result.PageResult;
import com.demo.result.Result;
import com.demo.dto.DepartmentDTO;
import com.demo.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin(origins = "http://localhost:8080")
@RequestMapping("/admin/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    //新增二级学院信息
    @PostMapping
    public Result save(@RequestBody DepartmentDTO departmentDTO) {
        departmentService.save(departmentDTO);
        return Result.success();
    }

    //分页查询
    @PostMapping("/page")
    public Result<PageResult> page(@RequestBody DepartmentPageQueryDTO departmentPageQueryDTO) {
        System.out.println(departmentPageQueryDTO);
        PageResult pageResult = departmentService.pageQuery(departmentPageQueryDTO);
        return Result.success(pageResult);
    }

    //删除二级学院信息
    @DeleteMapping("/id")
    public Result deleteById(@RequestParam("depID") String depID){
        departmentService.deleteById(depID);
        return Result.success();
    }

    /**
     * 批量删除
     * @param depIDs
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestBody String[] depIDs){
        System.out.println(depIDs);
        departmentService.delete(depIDs);
        return Result.success();
    }

    //修改二级学院信息
    @PutMapping
    public Result update(@RequestBody DepartmentDTO departmentDTO){
        departmentService.update(departmentDTO);
        return Result.success();
    }
}
